﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;
using BigTestV3.common;
using BigTestV3.xmlHelp;
using FilePath = BigTestV3.xmlHelp.FilePath;

namespace BigTestV3
{
    public partial class frmBasicSet : Form
    {
        public frmBasicSet()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 基本设置事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BasicSetClick(object sender, EventArgs e)
        {
            string stuPath = txtStuPath.Text.Trim();
            string tecPath = txtTecPath.Text.Trim();
            string setIp = txtSetIp.Text.Trim();
            string filePath = txtFilePath.Text.Trim();

            if (Validate(stuPath, tecPath, setIp, filePath))
            {
                string path = filePath + FilePathTotal.Setting;

                BasicSetting setting = new BasicSetting { FilePath = filePath, StuPath = stuPath, TecPath = tecPath, Ip = setIp };
                FilePath filepath = new FilePath() { Path = path };

                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                try
                {
                    XmlHelper.XmlSerializeToFile(setting, path, Encoding.UTF8);
                    XmlHelper.XmlSerializeToFile(filepath, FilePathTotal.PathPath, Encoding.UTF8);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                if (!Directory.Exists(tecPath))
                {
                    Directory.CreateDirectory(tecPath);
                }
                if (File.Exists(path))
                {
                    msgDiv.MsgDivShow("设置成功");
                }
                else
                {
                    msgDiv.MsgDivShow("设置失败，请重新设置");
                }
            }
        }

        /// <summary>
        /// 验证是否为空
        /// </summary>
        /// <param name="stuPath"></param>
        /// <param name="tecPath"></param>
        /// <param name="setIp"></param>
        /// <returns></returns>
        private bool Validate(string stuPath, string tecPath, string setIp, string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                msgDiv.MsgDivShow("文件地址不能为空");
            }

            else if (string.IsNullOrEmpty(tecPath))
            {
                msgDiv.MsgDivShow("学生答案提交路径不能为空");
                return false;
            }
            else if (string.IsNullOrEmpty(setIp))
            {
                msgDiv.MsgDivShow("Ip地址不能为空");
                return false;
            }
            else if (string.IsNullOrEmpty(stuPath))
            {
                msgDiv.MsgDivShow("学生答案存放路径不能为空");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 获取IP地址
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetLocalIpClick(object sender, EventArgs e)
        {
            try
            {
                IPHostEntry ipHE = Dns.GetHostByName(Dns.GetHostName());
                txtSetIp.Text = ipHE.AddressList[0].ToString();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Ip地址获取失败");
            }
        }


    }
}
